package com.runen.infinitemovement.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MemberEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.infinitemovement.widget.imageview.RoundedCornersImage;
import com.runen.rxnetlibrary.utils.TimeUtil;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class MemberAdapter extends BaseQuickAdapter<MemberEntity.DataBean.TeamMemberListBean, BaseViewHolder> {
    private Context context;
    private TextView tv_leader, tv_member_name, tv_time, tv_hozom_num, tv_by, tv_tx;
    private ImageView iv_by, iv_tx;
    private RoundedCornersImage rci_captain_header;
    private LinearLayout ll_by, ll_tx;

    private String createTime = "加入时间：%s";
    private OnClick onClick;
    private String leaderId;

    public OnClick getOnClick() {
        return onClick;
    }

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }

    public MemberAdapter(Context context, String leaderId) {
        super(R.layout.item_member);
        this.context = context;
        this.leaderId = leaderId;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, MemberEntity.DataBean.TeamMemberListBean memberListBean) {
        tv_leader = baseViewHolder.findView(R.id.tv_leader);
        tv_member_name = baseViewHolder.findView(R.id.tv_member_name);
        tv_time = baseViewHolder.findView(R.id.tv_time);
        tv_hozom_num = baseViewHolder.findView(R.id.tv_hozom_num);
        tv_by = baseViewHolder.findView(R.id.tv_by);
        tv_tx = baseViewHolder.findView(R.id.tv_tx);
        iv_by = baseViewHolder.findView(R.id.iv_by);
        iv_tx = baseViewHolder.findView(R.id.iv_tx);
        rci_captain_header = baseViewHolder.findView(R.id.rci_captain_header);
        ll_by = baseViewHolder.findView(R.id.ll_by);
        ll_tx = baseViewHolder.findView(R.id.ll_tx);

        GlideUtils.loadImage(context, memberListBean.headImgUrl, rci_captain_header);

        tv_leader.setVisibility(leaderId.equals(memberListBean.userId) ? View.VISIBLE : View.GONE);

        if (leaderId.equals(UserAPI.getUserId())) {
            ll_tx.setVisibility(View.VISIBLE);
        } else
            ll_tx.setVisibility(View.GONE);
        tv_member_name.setText(memberListBean.nickname);
        tv_time.setText(String.format(createTime, TimeUtil.dealDateFormat2(memberListBean.createTime)));
        String text = "%s颗";
        tv_hozom_num.setText(String.format(text, memberListBean.tinder));

        iv_by.setImageResource(memberListBean.isLikeStatus ? R.mipmap.ico_my_team_by_select : R.mipmap.ico_my_team_by_unselect);
        iv_tx.setImageResource(memberListBean.isRemindStatus ? R.mipmap.ico_my_team_tx_select : R.mipmap.ico_my_team_tx_unselect);

//        ll_by.setEnabled(!memberListBean.isLikeStatus);
//        ll_tx.setEnabled(!memberListBean.isRemindStatus);

        tv_by.setText(memberListBean.likeNumber + "");
        tv_tx.setText(memberListBean.remindNumber + "");

        ll_by.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null)
                    onClick.like(getItemPosition(memberListBean));
            }
        });

        ll_tx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClick != null)
                    onClick.remind(getItemPosition(memberListBean));
            }
        });
    }

    public interface OnClick {
        void like(int position);

        void remind(int position);
    }
}
